﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZH.Infrastructure.Attr;
using ZH.Infrastructure.Dto.Request.Pro;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Dto.Response.Pro;
using ZH.Infrastructure.Dto.Response.Tech;
using ZH.Infrastructure.EFCore.MySql;
using ZH.Infrastructure.IOC;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;

namespace ZH.Service.Instance
{
    [Inject_]
    public class Pro_Service : Base_Service, I_Pro_Service
    {
        public readonly Pro_IOC _pro_IOC;
        public readonly IConfiguration _configuration;

        public Pro_Service(Pro_IOC pro_IOC,
            IConfiguration configuration
            )
        {
            _pro_IOC = pro_IOC;
            _configuration = configuration;
        }

        public async Task<Api_Response_Dto> Add_Pro(Pro_Request_Dto dto)
        {
            Product ls=new Product();
            ls.Pid = Config.GUID2();
            ls.Pname=dto.name;
            ls.Pcover=dto.pcover;
            ls.Pspecs=dto.pspecs;
            ls.Price=dto.price;
            ls.Ptype=dto.ptype;
            ls.PisBan = 1;
            ls.Pcreationtime= DateTime.Now;
            ls.PDetails=dto.pDetails;
            ls.Inventory = 0;
            ls.Earlywarning = 100;
             _pro_IOC._pro_EFCore.Add(ls);
           var code=await _pro_IOC._pro_EFCore.SaveChangesAsync();
            return Result(code, "添加成功");
        }
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Delete_Pro(string? id)
        {
            var ls = await _pro_IOC._pro_EFCore.QueryAll(d=>d.Pid==id).FirstOrDefaultAsync();
            if (ls == null) {
                return Result(1, "未找到该产品"); 
            }
            _pro_IOC._pro_EFCore.Delete(ls);
            var code=_pro_IOC._pro_EFCore.SaveChanges();
            return Result(code, "删除成功");
        }
        /// <summary>
        /// 禁用/启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> DS_Pro(string? id)
        {
            var ls = await _pro_IOC._pro_EFCore.QueryAll(d => d.Pid == id).FirstOrDefaultAsync();
            if (ls == null) {
                return Result(1, "未找到该产品");
            } 
            ls.PisBan = ls.PisBan == 1 ? 0 : 1;
            _pro_IOC._pro_EFCore.Update(ls);
            var code = _pro_IOC._pro_EFCore.SaveChanges();
            return Result(code, "操作成功");

        }

        /// <summary>
        /// 获取产品列表(无分页)
        /// </summary>
        /// <returns></returns>
        public async  Task<Api_Response_Dto> Get_Cashier_Pro()
        {
            var data = await _pro_IOC._pro_EFCore.QueryAll().Select(d => new Pro_Response_Dto
                   {
                       Pid = d.Pid,
                       Pname = d.Pname,
                       Ptype = d.Ptype,
                       pcover = d.Pcover,
                       Pcreationtime = d.Pcreationtime!.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                       pspecs = d.Pspecs,
                       Price = (decimal)d.Price,
                       PisBan = (int)d.PisBan,
                       PDetails = d.PDetails,
                       Pinventory = (int)d.Inventory,
                       PEarlywarning = (int)d.Earlywarning,
                   }).ToListAsync();

            var total = data.Count();

            var type = _configuration["Dictionary_Type:product"];
            var dic = _pro_IOC._dictionary_EFCore.QueryAll(d => d.DparentId == type).ToList();
            var query = from d in data
                        join c in dic on d.Ptype equals c.Did
                        select new Pro_Response_Dto
                        {
                            Pid = d.Pid,
                            Pname = d.Pname,
                            Ptype = d.Ptype,
                            pcover = d.pcover,
                            Pcreationtime = d.Pcreationtime,
                            pspecs = d.pspecs,
                            Price = d.Price,
                            PisBan = d.PisBan,
                            PDetails = d.PDetails,
                            PtypeName = c.Dname,
                            Pinventory = d.Pinventory,
                            PEarlywarning = d.PEarlywarning,
                        };
            return Result(1, "ok", new{ total,query });
        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Get_pro(Pro_Response_Dto dto)
        {

            var data = await _pro_IOC._pro_EFCore.QueryAll(out int total,
                  Convert.ToInt32(dto.page), Convert.ToInt32(dto.limit),
                  true,
                  o => o.Pid,
                    d => (string.IsNullOrEmpty(dto.key) || d.Pname.Contains(dto.key))
                  ).Select(d => new Pro_Response_Dto
                  {
                      Pid = d.Pid,
                      Pname = d.Pname,
                      Ptype = d.Ptype,
                      pcover = d.Pcover,
                      Pcreationtime = d.Pcreationtime!.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                      pspecs= d.Pspecs,
                      Price =(decimal)d.Price,
                      PisBan=(int)d.PisBan,
                      PDetails=d.PDetails,
                      Pinventory =(int)d.Inventory,
                      PEarlywarning=(int)d.Earlywarning,
                  }).ToListAsync();

            var type = _configuration["Dictionary_Type:product"];
            var dic = _pro_IOC._dictionary_EFCore.QueryAll(d => d.DparentId == type).ToList();
            var query = from d in data
                        join c in dic on d.Ptype equals c.Did
                        select new Pro_Response_Dto
                        {
                            Pid = d.Pid,
                            Pname = d.Pname,
                            Ptype = d.Ptype,
                            pcover = d.pcover,
                            Pcreationtime = d.Pcreationtime,
                            pspecs = d.pspecs,
                            Price = d.Price,
                            PisBan = d.PisBan,
                            PDetails = d.PDetails,
                            PtypeName = c.Dname,
                            Pinventory = d.Pinventory,
                            PEarlywarning = d.PEarlywarning,
                        };
            return Result(1, "ok", new
            {
                total,
                query
            });
        }


        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Response_Dto> Update_Pro(Pro_Request_Dto dto)
        {
            var ls = await _pro_IOC._pro_EFCore.QueryAll(d => d.Pid == dto.pid).FirstOrDefaultAsync();

            if (ls == null) {
                return Result(1, "未找到该产品");
            }
            ls.Pname=dto.name;
            ls.Pcover=dto.pcover;
            ls.Pspecs=dto.pspecs;
            ls.Ptype=dto.ptype;
            ls.Price=dto.price;
            ls.PDetails=dto.pDetails;
            _pro_IOC._pro_EFCore.Update(ls);
         var code= _pro_IOC._pro_EFCore.SaveChanges();
            return Result(code, "修改成功");
        }
    }
}
